package com.agent.action.controller;

import com.agent.action.mapper.PractiseCatalogMapper;
import com.agent.action.param.PractiseParam;
import com.agent.action.service.PractiseService;
import com.agent.action.vo.PractiseVO;
import com.agent.action.vo.PractisesCatalogVO;
import com.agent.commons.Result.Result;
import com.agent.commons.exception.BaseException;
import com.agent.commons.other.ThreadLocal;
import jakarta.annotation.Resource;
import org.aspectj.weaver.ast.Var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import static com.agent.commons.errorcode.ServiceErrorCodeConstants.*;

import java.util.List;

/**
 * 生成练习
 */
@RestController
@RequestMapping("/practise")
public class PractiseController {
    @Resource
    private PractiseService practiseService;
    @Resource
    private PractiseCatalogMapper practiseCatalogMapper;

    /**
     * 保存练习题目细节(包括修改)
     * @param practiseParam
     * @return
     */
    @PostMapping("/load")
    public Result<List<PractiseVO>> generatePractise(@RequestBody PractiseParam practiseParam){
        if(practiseParam.getPractiseId()!=null&&!practiseService.containPractise(practiseParam.getPractiseId())){
            throw new BaseException(DATA_NOT_FOUND);
        }
        List<PractiseVO> practisesCatalogVOS = practiseService.loadPractise(practiseParam);
        return Result.success(practisesCatalogVOS);
    }

    /**
     * 获取所有题集列表
     * @return
     */
    @GetMapping("/list/catalog")
    public Result<List<PractisesCatalogVO>> getPractisesCatalogList(){
        List<PractisesCatalogVO> practisesCatalogVOS =practiseService.listPractisesCatalog(ThreadLocal.getUserId());
        return Result.success(practisesCatalogVOS);
    }

    /**
     * 删除题集
     */
    @DeleteMapping("/delete/{catalogId}")
    public Result<List<PractisesCatalogVO>> deletePractisesCatalog(@PathVariable String catalogId){
        boolean delete = practiseService.deletePractisesCatalog(catalogId);
        if(delete){
            return Result.success(practiseService.listPractisesCatalog(ThreadLocal.getUserId()));
        }
        return Result.error("删除失败！");
    }

    /**
     * 获取题集详细题目
     */
    @GetMapping("/list/practise/{practiseId}")
    public Result<List<PractiseVO>> getPractises(@PathVariable String practiseId){
        if(!practiseService.belongToUser(practiseId)){
            throw new BaseException(USER_NOT_CATALOG);
        }
        List<PractiseVO> practises = practiseService.getPractises(practiseId);
        return Result.success(practises);
    }


    /**
     * 上传图片
     * @param files
     * @return
     */
    @PostMapping("/upload/picture")
    public Result<List<String>> uploadPicture(@RequestPart MultipartFile[] files){
        List<String> url = practiseService.loadPhoto(files);
        return Result.success(url);
    }
}
